package com.trainingcloud.action.document;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.trainingcloud.XMLSerializer;
import com.trainingcloud.action.BaseSessionAction;
import com.trainingcloud.dao.DocumentDAO;
import com.trainingcloud.entity.ClientDocument;
import com.trainingcloud.entity.Document;
import com.trainingcloud.exception.DAOException;
import com.trainingcloud.exception.SystemException;

public class UploadAction extends BaseSessionAction
{
	private static final int BUFFER_SIZE=16*1024;
	private File docFile;
	private String docFileContentType;
	private String docFileFileName;
	private String docTitle;
	private boolean docShared;
	
	public UploadAction() throws SystemException 
	{
		super();
	}
	
	public void copyFile(File source,File target) throws SystemException
	{
		InputStream in = null;
		OutputStream out = null;
		try 
		{
			in = new BufferedInputStream(new FileInputStream(source), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(target), BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			while (in.read(buffer) > 0) 
			{
				out.write(buffer);
			}
		}
		catch (IOException ex) 
		{
			throw new SystemException("Copy file failed.", ex);
		}
		finally
		{
			try
			{
				in.close();
				out.close();
			}
			catch (IOException ex)
			{
				throw new SystemException("Stream close failed.", ex);
			}
		}
	}
	
	private String generateFilename()
	{
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	@Override
	public String callWithSession() throws DAOException, SystemException 
	{
		String filename = this.generateFilename();
		String fileLocation = ServletActionContext.getServletContext().getRealPath("/documents")+"/"+filename;
		
		Document document = new Document();
		document.setTitle(docTitle);
		document.setFilename(docFileFileName);
		document.setMimeType(docFileContentType);
		document.setFileLocation(filename);
		document.setShared(docShared);
		document.setOwner(currentUser);
		document.setCreateTime(new Date());
		if(docShared)
			document.setSharedTime(new Date());
		
		File targetFile = new File(fileLocation);
		copyFile(docFile, targetFile);
		
		DocumentDAO documentDAO = new DocumentDAO();
		documentDAO.insertDocument(document);
		
		ClientDocument clientDoc = new ClientDocument();
		clientDoc.setDocument(document);
		clientDoc.setFavorite(false);
		clientDoc.setLatestUsedTime(null);
		
		XMLSerializer.clientDocumentToXML(clientDoc, responseElement);
		
		return null;
	}

	public File getDocFile() {
		return docFile;
	}

	public void setDocFile(File docFile) {
		this.docFile = docFile;
	}

	public String getDocFileContentType() {
		return docFileContentType;
	}

	public void setDocFileContentType(String docFileContentType) {
		this.docFileContentType = docFileContentType;
	}

	public String getDocFileFileName() {
		return docFileFileName;
	}

	public void setDocFileFileName(String docFileFileName) {
		this.docFileFileName = docFileFileName;
	}

	public String getDocTitle() {
		return docTitle;
	}

	public void setDocTitle(String docTitle) {
		this.docTitle = docTitle;
	}

	public boolean isDocShared() {
		return docShared;
	}

	public void setDocShared(boolean docShared) {
		this.docShared = docShared;
	}
}
